package com.fast.alden.admin.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Component;

/**
 * 定义动态鉴权方法切面，计算执行时间
 */
@Aspect
@Component
@Slf4j
@Profile("dev")
public class DynamicAuthorizationTakeTime {
    /**
     * 定义切入点
     */
    @Pointcut("execution(* com.fast.alden.admin.security.service.DynamicAuthorizationService.check(..))")
    public void performance() {

    }

    /**
     * 在方法执行前后获取时间戳，记录消耗时间
     */
    @Around("performance()")
    public Object logTakeTime(ProceedingJoinPoint joinPoint) throws Throwable {
        // 记录起始时间
        long begin = System.currentTimeMillis();
        try {
            return joinPoint.proceed();
        } finally {
            // 计算消耗时间
            long took = System.currentTimeMillis() - begin;
//            log.info("动态鉴权执行时间为: {}秒", took / 1000.0);
        }
    }
}
